package leetcode.binarysearch;
//162. 寻找峰值
public class FindPeakElement {

    public static void main(String[] args) {

        int[] data={1,2,3};
        System.out.println(findPeakElementP(data));
    }
    public static int findPeakElement(int[] nums) {
        int left=0;
        int right = nums.length-1;
        while (left<right){
            int mid = (left+right)/2;
            if (nums[mid]<nums[mid+1]){  //说明峰值在右侧
                left = mid+1;
            }else {
                right = mid;
            }
        }
        //程序运行到这，俩指针合并
        return left;
    }


    //暴力
    public static int findPeakElementP(int[] nums) {

        for (int i = 0; i < nums.length-1; i++) {
            if (nums[i]>nums[i+1]){
                return i;
            }
        }
        return nums.length-1;
    }

    //二分
    public static int findPeakElementP2(int[] nums) {
            int l = 0;
            int r = nums.length-1;
            while (l<r){
                int mid = l+(r-l)/2;
                if (nums[mid]<nums[mid+1]){
                    l = mid+1;
                }else {
                    r = mid;
                }
            }
            //指针重合
            return l;
    }
}
